﻿using Lost_and_Found.Models.DB;
using Lost_and_Found.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Lost_and_Found.Models.ObjectManager
{
    public class UserManager
    {
        LAFEntities laf = new LAFEntities();
        public void Add(RegisterModel user)
        {
            User myUser = new User();
            myUser.Username = user.Username;
            myUser.Password = CalculateMD5Hash(user.Password);
            myUser.Email = user.Email;
            myUser.Fullname = user.Fullname;
            myUser.SClass = user.SClass;
            myUser.SIndex = user.SIndex;
            myUser.SGrade = user.SGrade;
            myUser.Phone = user.Phone;
            myUser.SInstitute = user.SInstitute;
            myUser.DateRegis = DateTime.Now;
            myUser.Roles = "Member";
            laf.Users.Add(myUser);
            laf.SaveChanges();
        }

        public void Add(AdminModel user)
        {
            User myUser = new User();
            myUser.Username = user.Username;
            myUser.Password = CalculateMD5Hash(user.Password);
            myUser.Email = user.Email;
            myUser.Fullname = user.Fullname;
            myUser.DateRegis = DateTime.Now;
            myUser.Roles = user.Role;
            laf.Users.Add(myUser);
            laf.SaveChanges();
        }

        public bool ValidateUser(LoginModel user)
        {

            User tempUser;
            try
            {
                tempUser = laf.Users.Single(m => m.Username == user.Username);
            }
            catch (System.InvalidOperationException)
            {
                return false;
            }
            if (tempUser.Password == CalculateMD5Hash(user.Password))
            {
                return true;
            }
            return false;
        }

        private string CalculateMD5Hash(string input)
        {
            MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
            byte[] hash = md5.ComputeHash(inputBytes);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hash.Length; i++)
            {
                sb.Append(hash[i].ToString("X2"));
            }
            return sb.ToString();
        }

        public bool IsUserLoginIDExist(string userLogIn)
        {
            return (from o in laf.Users where o.Username == userLogIn select o).Any();
        }

        public static User getUser(String name)
        {
            LAFEntities laf = new LAFEntities();
            User user = new User();
            try
            {
                user = laf.Users.Single(m => m.Username.Equals(name));
                return user;
            }
            catch
            {
                return null;
            }
        }
    }
}